<template>
  <d-scroll-view  @refresherrefresh="getDetail" :refresher-triggered="triggered">
    <proj-detail-layout :process-instance-id="processInstanceId" :project-number="projectNumber">
    <d-forms label-width="180rpx">
      <d-forms-item label="采购编号" :input="form.code"></d-forms-item>
      <d-forms-item label="申请人" :input="form.applicantNickname"></d-forms-item>
      <d-forms-item label="采购事由" :input="form.reason"></d-forms-item>
      <d-forms-item label="期望交付日期" :type="DFormInputType.DATE" :input="form.exceptDeliveryDate"></d-forms-item>
      <d-forms-item label="相关资料">
        <view class="link-text" @click="gotoPurchaseList">《合同采购清单》</view>
        <view v-if="form.visaIdList">
          <view
            class="link-text"
            v-for="(visa, idx) in form.visaIdList"
            :key="visa.id"
            @click="gotoWorkVisaDetail(visa.id)">
            {{ `《签证-${visa.visaBatch}》` }}
          </view>
        </view>
      </d-forms-item>
      <view>
        <view class="form-title-1">采购物品信息</view>
        <d-card class="mb10" v-for="(item, idx) in form.itemList" :key="`purchase-item_`+idx">
          <d-forms-item disabled label="采购类型" :type="DFormInputType.DICT" :dict-type="PM_DICT_TYPE.PURCHASE_ITEM_TYPE" :input="item.purchaseType"></d-forms-item>
          <d-forms-item disabled label="物品名称" :input="item.itemName"></d-forms-item>
          <d-forms-item disabled label="是否合同内" :type="DFormInputType.DICT" :dict-type="PM_DICT_TYPE.PURCHASE_CONTRACT_TYPE" :input="item.embodyContract"></d-forms-item>
          <d-forms-item disabled label="品牌/厂商" :input="item.firmName"></d-forms-item>
          <d-forms-item disabled label="型号" :input="item.model"></d-forms-item>
          <d-forms-item disabled label="申请数量" :input="item.num"></d-forms-item>
          <d-forms-item disabled label="参数规格" :input="item.specifications"></d-forms-item>
        </d-card>
      </view>
      <view>
        <view class="form-title-1">收货信息</view>
        <d-card>
          <d-forms-item
            disabled
            label-width="160rpx"
            label="签收方"
            :type="DFormInputType.DICT"
            :dict-type="PM_DICT_TYPE.PURCHASE_APPLY_RECEIVER"
            :input="form.receiver"
          ></d-forms-item>
          <d-forms-item disabled label-width="160rpx" label="联系人" :input="form.receiverLinkMan"></d-forms-item>
          <d-forms-item disabled label-width="160rpx" label="联系电话" :input="form.receiverLinkPhone"></d-forms-item>
          <d-forms-item disabled label-width="160rpx" label="地址" :input="form.receiverAddr"></d-forms-item>
        </d-card>
      </view>
      <view>
        <view class="form-title-1">补充说明</view>
        <d-card>
          <d-forms-item disabled :input="form.supplement"></d-forms-item>
        </d-card>
      </view>
      <view>
        <view class="form-title-1">附件</view>
        <d-card>
          <pm-file-upload readonly :files="form.attach"></pm-file-upload>
        </d-card>
      </view>
    </d-forms>
  </proj-detail-layout>
  </d-scroll-view>
</template>

<script setup lang="ts">
import ProjDetailLayout from "@/components/ProjDetailLayout/index.vue";
import {usePurchaseApplyApi} from "@/api/pm/project/purchaseApply";
import {DFormInputType} from "@/utils/enum";
import {PM_DICT_TYPE} from "@/utils/pmDict";
import {queryToUrl} from "@/utils";
import {RouteNameEnum} from "@/utils/pm/pmRoute";

defineOptions({ name: 'PurchaseApply' })

const initForm = {
  projectNumber: null,
  reason: null,
  exceptDeliveryDate: null,
  attach: null,
  supplement: null,
  receiver: null,
  receiverLinkMan: null,
  receiverLinkPhone: null,
  receiverAddr: null,
  storaged: null,
  id: null,
  code: null,
  applicant: null,
  trends: null,
  currentNode: null,
  processInstanceId: null,
  result: null,
  pushStatus: null,
  projectName: null,
  projectType: null,
  applicantNickname: null,
  deptName: null,
  itemList: null,
  visaIdList: null,
}
const form = ref<PurchaseApplyForm>(initForm);
const triggered = ref<boolean>(false);
function getDetail() {
  if (processInstanceId.value) {
    triggered.value = true;
    usePurchaseApplyApi().getPAByProcessInstanceId(processInstanceId.value).then(res => {
      if (res.code === 0) {
        form.value = res.data || initForm
      }
    }).finally(() => {
      triggered.value = false;
    })
  }
}

function gotoPurchaseList() {
  uni.navigateTo({
    url: queryToUrl('/pmPages/project/detail/index', {
      component_name: RouteNameEnum.ContractPurchaseList,
      project_number: projectNumber.value,
    })
  })
}

function gotoWorkVisaDetail(id: number) {
  uni.navigateTo({
    url: queryToUrl('/pmPages/project/detail/index', {
      component_name: RouteNameEnum.WorkVisaDetail,
      project_number: projectNumber.value,
      id: id,
    })
  })
}

const projectNumber = ref<string>(null);
const processInstanceId = ref<string>(null);
onLoad((query) => {
  projectNumber.value = query['project_number']
  processInstanceId.value = query['process_instance_id']
  getDetail();
})
</script>

<style scoped lang="scss">

</style>
